package com.yisu.expressway.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yisu.expressway.R;
import com.yisu.expressway.ui.loading.CustomProgressDialog;
import com.yisu.expressway.ui.recyclerview.PagingRecyclerView;
import com.yisu.expressway.utils.DisplayUtil;


import java.text.SimpleDateFormat;

/**
 * Created by theodore_hu on 2016/1/24.
 */
public class BaseFragment extends Fragment implements View.OnClickListener{

    public int mScreenWidth;
    protected SimpleDateFormat sdf;

    protected LinearLayout mEmptyView;
    protected TextView mTvEmpty;
    CustomProgressDialog mLoadingDialog;
    protected int NavigationBarHeight = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        mScreenWidth = DisplayUtil.getScreenWidth(getActivity());
        mLoadingDialog = new CustomProgressDialog(getActivity());
        NavigationBarHeight = DisplayUtil.getNavigationBarSize(getActivity()).y;
    }

    protected void startTargetActivity(Class targetClass){
        Intent mIntent = new Intent(getActivity(), targetClass);
        startActivity(mIntent);
    }

    @Override
    public void onClick(View v) {

    }

    protected int getAdjustGridViewSize(Context context){
        int realsize = 0;
        int mScreenWidth = DisplayUtil.getScreenWidth(context);
        realsize = (mScreenWidth - 4)/2;
        return realsize;
    }


    protected void adjustViewHeight(View view,int height){
        ViewGroup.LayoutParams mLayoutParams = view.getLayoutParams();
        if (mScreenWidth == 0) {
            mScreenWidth = DisplayUtil.getScreenWidth(getActivity());
        }
        mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        int minHeight = DisplayUtil.dip2px(getActivity(),179);
        int maxHeight = DisplayUtil.dip2px(getActivity(),240);
        if(height < minHeight){
            height = minHeight;
        }else if(height > maxHeight){
            height = maxHeight;
        }
        mLayoutParams.height = height;
        view.setLayoutParams(mLayoutParams);
    }

//    protected void adjustDefaultViewHeight(View view){
//        ViewGroup.LayoutParams mLayoutParams = view.getLayoutParams();
//        if (mScreenWidth == 0) {
//            mScreenWidth = DisplayUtil.getScreenWidth(getActivity());
//        }
//        mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        mLayoutParams.height = mModuleItemWidth;
//        view.setLayoutParams(mLayoutParams);
//    }

    /**
     * 列表为空时,添加空列表提示
     * @param stringId 提示文案id
     * @param recyclerView
     * @param divider
     */
    protected void addEmptyView (int stringId, PagingRecyclerView recyclerView,
                                 RecyclerView.ItemDecoration divider) {
        // If the fragment is not attached to an activity,
        // IllegalStateException (Fragment XXX not attached to Activity) will be thrown.
        if (!isAdded())
            return;
        addEmptyView(getString(stringId), recyclerView, divider);
    }

    /**
     * 列表为空时,添加空列表提示
     * @param msg 提示文案
     * @param recyclerView
     * @param divider
     */
    protected void addEmptyView (String msg, PagingRecyclerView recyclerView,
                                 RecyclerView.ItemDecoration divider) {
        if (recyclerView == null || recyclerView.getDataAdapter().getItemCount() > 0) {
            return;
        }

        mEmptyView = (LinearLayout) LayoutInflater.from(getActivity())
                .inflate(R.layout.empty_list_layout, recyclerView, false);
        mTvEmpty = (TextView) mEmptyView.findViewById(R.id.tv_empty_list);
        mTvEmpty.setText(msg);
        recyclerView.getDataAdapter().setHeaderView(mEmptyView);
        if (divider != null) {
            recyclerView.removeItemDecoration(divider);
        }
    }

    /**
     * 列表不为空时,移除空列表提示
     */
    protected void removeEmptyView(PagingRecyclerView recyclerView,
                                   RecyclerView.ItemDecoration divider) {
        if(recyclerView == null) {
            return ;
        }

        if (mEmptyView != null) {
            if (divider != null) {
                recyclerView.removeItemDecoration(divider);
                recyclerView.addItemDecoration(divider);
            }
            recyclerView.getDataAdapter().removeHeader();
            mEmptyView = null;
        }
    }

    /**
     * 显示默认loading
     */
    protected void showLoading() {
//        ActivityLoading.showLoading(this, R.layout.activity_loading_simple);
        if(mLoadingDialog != null){
            mLoadingDialog.show();
            mLoadingDialog.showLoading();
        }
    }

    /**
     * 关闭loading
     */
    protected void dismissLoading() {
//        ActivityLoading.dismiss(this);
        if(mLoadingDialog != null){
            mLoadingDialog.dismissLoading();
            mLoadingDialog.dismiss();
        }
    }


}
